<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>SSM框架整合</title>
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../elementui/index.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1><small>账户管理</small></h1>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="userName" label="姓名" align="center"></el-table-column>
                        <el-table-column prop="sex" label="性别" align="center"></el-table-column>
                        <el-table-column prop="homeAddress" label="地址" align="center"></el-table-column>

                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增账户" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData"  label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="姓名">
                                            <el-input v-model="formData.userName" type="text"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="性别">
                                            <el-input v-model="formData.sex" type="text"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="地址">
                                            <el-input v-model="formData.homeAddress" type="text"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑账户" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataAddForm" :model="formData"  label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="姓名">
                                            <el-input v-model="formData.userName" type="text"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="性别">
                                            <el-input v-model="formData.sex" type="text"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="地址">
                                            <el-input v-model="formData.homeAddress" type="text"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
				dataList: [
                     {id:1,userName:"刘备",sex:'男', homeAddress: "蜀国"},
                     {id:2,userName:"关羽",sex:'男', homeAddress: "荆州"},
                     {id:3,userName:"张飞",sex:'男', homeAddress: "冀州"}
                ],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false//编辑表单是否可见
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                this.findAll();
            },
            methods: {
                //编辑
                handleEdit() {
                    axios.put("/user",this.formData).then((res)=>{
                        this.$message({
                            type: res.data.flag ? "success":"error",
                            message:res.data.message
                        });
                        this.dialogFormVisible4Edit = false;
                        this.findAll();
                    });
                },
                //添加
                handleAdd (){
                    axios.post("/user",this.formData).then((res)=>{
                        this.$message({
                            type: res.data.flag ? "success":"error",
                            message:res.data.message
                        });
                        this.dialogFormVisible = false;
                        this.findAll();
                    });
                },
                //查询
                findAll() {
                    axios.get("/user").then((res)=>{
                        this.dataList = res.data.result;
                        if(!res.data.flag){
                            this.$message({
                                type: "error",
                                message:res.data.message
                            });
                        }

                    });
                },
                // 新增弹窗
                handleCreate() {
                    this.dialogFormVisible = true;
					this.formData = {};
                },
                //编辑弹框
                handleUpdate(row) {
                    this.dialogFormVisible4Edit = true;
                    axios.get("/user/" + row.id).then((res)=>{
                        this.formData = res.data.result;
                    });
                },
                // 删除
                handleDelete(row) {
                    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        axios.delete("/user/" + row.id).then((res)=>{
                            this.$message({
                                type: res.data.flag ? "success":"error",
                                message:res.data.message
                            });
                            this.findAll();
                        });

                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            }
        })
    </script>
</html>